import cv2
from PIL import Image

img = cv2.imread('origin.png', 0)
cv2.imshow('image', img)
eva = cv2.resize(img, (500, 500))

img = Image.open('origin.png')
Img = img.convert('L')
Img.save("gray.jpg")

threshold = 105

table = []
for i in range(256):
    if i < threshold:
        table.append(1)
    else:
        table.append(0)

photo = Img.point(table, '1')
photo.save("digit.jpg")
cv2.waitKey(0)

cv2.destroyAllWindows()














































































































'''
#第一种方法
import cv2 as cv
image = cv.LoadImage('11.jpg')
grayimg= cv.CreateImage(cv.GetSize(image), image.depth, 1)
for i in range(image.height):
    for j in range(image.width):
        grayimg[i,j] = max(image[i,j][0], image[i,j][1], image[i,j][2])
cv.ShowImage('srcImage', image)
cv.ShowImage('grayImage', grayimg)
cv.WaitKey(0)
'''
# 分割字符
"""
# 记录黑白像素总和
white = []  # 记录每一列的白色像素总和
black = []  # 记录每一列的黑色像素总和
height = thresh.shape[0]  # 263
width = thresh.shape[1]  # 400
white_max = 0  # 仅保存每列，取列中白色最多的像素总数
black_max = 0  # 仅保存每列，取列中黑色最多的像素总数
# 计算每一列的黑白像素总和
for i in range(width):
    line_white = 0  # 这一列白色总数
    line_black = 0  # 这一列黑色总数
    for j in range(height):
        if thresh[j][i] == 255:
                line_white += 1
        if thresh[j][i] == 0:
                line_black += 1
    white_max = max(white_max, line_white)
    black_max = max(black_max, line_black)
    white.append(line_white)
    black.append(line_black)
    print('white_max', white_max)
    print('black_max', black_max)
"""